package nju.software.tagmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import nju.software.tagmanager.common.exception.CategoryTagRelationAlreadyExistsException;
import nju.software.tagmanager.common.exception.DuplicateCategoryTagRelationException;
import nju.software.tagmanager.common.result.Result;
import nju.software.tagmanager.pojo.entity.RelCategoryTag;
import nju.software.tagmanager.service.RelCategoryTagService;
import nju.software.tagmanager.mapper.RelCategoryTagMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 97942
* @description 针对表【rel_category_tag(类别-标签关系表)】的数据库操作Service实现
* @createDate 2024-01-12 16:03:24
*/
@Service
public class RelCategoryTagServiceImpl extends ServiceImpl<RelCategoryTagMapper, RelCategoryTag>
    implements RelCategoryTagService{

    @Resource
    private RelCategoryTagMapper relCategoryTagMapper;
    @Override
    public void addTagToCategory(Integer tagId, Integer categoryId) {
        List<RelCategoryTag> relCategoryTags = relCategoryTagMapper.selectList(new QueryWrapper<RelCategoryTag>()
                .eq("tag_id", tagId)
                .eq("category_id", categoryId));
        if (!relCategoryTags.isEmpty()) {
            if (relCategoryTags.size() > 1) {
                throw new DuplicateCategoryTagRelationException();
            }
            throw new CategoryTagRelationAlreadyExistsException();
        }

        RelCategoryTag rel = RelCategoryTag.builder()
                .categoryId(categoryId)
                .tagId(tagId)
                .build();
        relCategoryTagMapper.insert(rel);

    }
}




